package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.VoiceCheckStatus;
import com.yunkeji.common.utils.JsonUtils;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.CallVoiceCheck;
import com.yunkeji.dao.mapper.CallVoiceCheckMapper;
import com.yunkeji.dao.vo.CallVoiceCheckVo;
import com.yunkeji.system.domain.bo.CallVoiceCheckBo;
import com.yunkeji.system.domain.bo.CheckVoiceBo;
import com.yunkeji.system.service.ICallVoiceCheckService;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通话录音检查Service业务层处理
 *
 * @author yunkeji
 * @date 2024-08-18
 */
@RequiredArgsConstructor
@Service
public class CallVoiceCheckServiceImpl implements ICallVoiceCheckService {

    private final CallVoiceCheckMapper baseMapper;

    /** 查询通话录音检查 */
    @Override
    public CallVoiceCheckVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询通话录音检查列表 */
    @Override
    public TableDataInfo<CallVoiceCheckVo> queryPageList(CallVoiceCheckBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<CallVoiceCheck> lqw = buildQueryWrapper(bo);
//        Page<CallVoiceCheckVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<CallVoiceCheckVo> result = baseMapper.selectListPage(
            bo.getCallPlatformId(),
            bo.getAnswerPlatformId(),
            bo.getUcid(),
            bo.getStatus(),
            pageQuery.build());
        List<CallVoiceCheckVo> vos = result.getRecords();
        vos.forEach(vo -> vo.setKeyWords(JsonUtils.parseArray(vo.getKeyWord(), CallVoiceCheck.Details.class)));
        result.setRecords(vos);
        return TableDataInfo.build(result);
    }

    /** 查询通话录音检查列表 */
    @Override
    public List<CallVoiceCheckVo> queryList(CallVoiceCheckBo bo) {
        LambdaQueryWrapper<CallVoiceCheck> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CallVoiceCheck> buildQueryWrapper(CallVoiceCheckBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CallVoiceCheck> lqw = Wrappers.lambdaQuery();
        lqw.eq(
                bo.getCallPlatformId() != null,
                CallVoiceCheck::getCallPlatformId,
                bo.getCallPlatformId());
        lqw.eq(
                bo.getAnswerPlatformId() != null,
                CallVoiceCheck::getAnswerPlatformId,
                bo.getAnswerPlatformId());
        lqw.eq(StringUtils.isNotBlank(bo.getUcid()), CallVoiceCheck::getUcid, bo.getUcid());
        lqw.eq(
                StringUtils.isNotBlank(bo.getKeyWord()),
                CallVoiceCheck::getKeyWord,
                bo.getKeyWord());
        lqw.eq(bo.getStatus() != null, CallVoiceCheck::getStatus, bo.getStatus());
        lqw.eq(
                StringUtils.isNotBlank(bo.getVoiceUrl()),
                CallVoiceCheck::getVoiceUrl,
                bo.getVoiceUrl());
        lqw.eq(
                StringUtils.isNotBlank(bo.getVoiceText()),
                CallVoiceCheck::getVoiceText,
                bo.getVoiceText());
        lqw.orderByDesc(CallVoiceCheck::getCreateTime);
        return lqw;
    }

    /** 新增通话录音检查 */
    @Override
    public Boolean insertByBo(CallVoiceCheckBo bo) {
        CallVoiceCheck add = BeanUtil.toBean(bo, CallVoiceCheck.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改通话录音检查 */
    @Override
    public Boolean updateByBo(CallVoiceCheckBo bo) {
        CallVoiceCheck update = BeanUtil.toBean(bo, CallVoiceCheck.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(CallVoiceCheck entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除通话录音检查 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void pass(CheckVoiceBo bo) {
        List<Long> id = bo.getId();
        ArrayList<CallVoiceCheck> updates = new ArrayList<>();
        id.forEach(
                item -> {
                    CallVoiceCheck update = new CallVoiceCheck();
                    update.setId(item);
                    update.setStatus(VoiceCheckStatus.忽略.getCode());
                    updates.add(update);
                });
        baseMapper.updateBatchById(updates);
    }

    @Override
    public void refuse(CheckVoiceBo bo) {
        List<Long> id = bo.getId();
        ArrayList<CallVoiceCheck> updates = new ArrayList<>();
        id.forEach(
                item -> {
                    CallVoiceCheck update = new CallVoiceCheck();
                    update.setId(item);
                    update.setStatus(VoiceCheckStatus.违规.getCode());
                    updates.add(update);
                });
        baseMapper.updateBatchById(updates);
    }
}
